package com.elvis.lifecardemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Date;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    LeancloudTest leancloud;

    TextView textView;
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        leancloud = new LeancloudTest(this);

        editText = (EditText) findViewById(R.id.et_view);
        textView = (TextView) findViewById(R.id.tv_id);
        findViewById(R.id.btn_test).setOnClickListener(this);
        findViewById(R.id.btn_regist).setOnClickListener(this);
        findViewById(R.id.btn_login).setOnClickListener(this);
        findViewById(R.id.btn_send).setOnClickListener(this);
        findViewById(R.id.btn_postlist).setOnClickListener(this);
        findViewById(R.id.btn_followee).setOnClickListener(this);
        findViewById(R.id.btn_follower).setOnClickListener(this);
        findViewById(R.id.btn_userdata).setOnClickListener(this);
        findViewById(R.id.btn_comment).setOnClickListener(this);
        findViewById(R.id.btn_like).setOnClickListener(this);
        findViewById(R.id.btn_msg1).setOnClickListener(this);
        findViewById(R.id.btn_msg2).setOnClickListener(this);

    }

    String psw = "123456";
    String result;
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_test:
                leancloud.leanTest(); // 测试 SDK 是否正常
                break;
            case R.id.btn_regist:
                leancloud.register(editText.getText().toString(), psw);
//                leancloud.register("test002", psw);
                break;
            case R.id.btn_login:
                leancloud.login(editText.getText().toString(), psw);
//                leancloud.login("test001", psw);
                break;
            case R.id.btn_send:
                leancloud.post(new Date(System.currentTimeMillis())
                        +"_"+editText.getText().toString());
                break;
            case R.id.btn_postlist:
                leancloud.getMultiParamsQuery();
                break;
            case R.id.btn_followee:
                leancloud.follow();
                break;
            case R.id.btn_follower:
                leancloud.getFolloweeAndFollower();
                break;
            case R.id.btn_userdata:
                leancloud.getUserData();
                break;
            case R.id.btn_comment:
                leancloud.comment("comment:"+new Date(System.currentTimeMillis())
                        +"_"+editText.getText().toString());
                break;
            case R.id.btn_like:
                leancloud.like();
                break;
            case R.id.btn_msg1:
                leancloud.sendMsg1("comment:"+new Date(System.currentTimeMillis())
                        +"_"+editText.getText().toString());
                break;
            case R.id.btn_msg2:
                break;
        }
    }

    public void showMsg(String funName, final String msg) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(MainActivity.this, msg, Toast.LENGTH_SHORT).show();
            }
        });
        logMsg(funName, msg);
    }

    private void logMsg(String funName, String msg) {
        Log.e("--Main--", funName + "-->> " + msg);
        textView.setText(funName + "-->> " + msg);
    }

}
